require(['init'], function () {
//初始化显示
  getfullCalendar();

  function getappointrecords() {
      ajaxMethod({
          url: '/api/schedules/students',
          type: 'get',
          dataType: 'json',
          success: function (data) {
              studentChedule(data);
          }

      })
      //追加Dom
      function studentChedule(data) {
         // var $row = $('.row').html('');
          var $studentC = $('.studentC').html("")
          $.each(data, function (index, item) {
              function getDurationMinute(begindate, enddate) {
                  if (begindate != null || enddate != null) {
                      var beginTime = +new Date(begindate.replace('T', ' '));
                      var endTime = +new Date(enddate.replace('T', ' '));
                      return (endTime - beginTime) / (60 * 1000) + '分钟';
                  } else {
                      return '--';
                  }
              }

              var id = item.studentuid;
              var $tr = $('<tr class="" data-uid=' + item.id +' style="height:45px;"></tr>').prependTo($studentC);
              var $td = $('<td ></td>').appendTo($tr);
              var $detailedinformation = $('<a  data-uid='+id+'   data-name='+item.studentname+'>'+item.studentname+'</a>').appendTo($td);
              $tr.append('<td>' + item.mobile + '</td>')
              $tr.find('td').css('font-size', '13px');
              $tr.on('click',function(){
              $tr.addClass('active').siblings().removeClass('active');
                var name=$tr.find($detailedinformation).attr('data-name');
                var studentName=$('#studentName').html(name);
                if($("#calendar").html!=""){
                  $("#calendar").html("");
                  $('input[name="id"]').val(item.studentuid);
                  getfullCalendar();
                }
              })
            })
          }
        }

 getappointrecords();
 //日历展示
 var _id;
 var initialLangCode = 'en';
 function getfullCalendar(){
     if (!localStorage.lang) {
         localStorage.setItem('lang', 'zh');
     }
     var dayNamesShort;
     var titleFormat;

     if (localStorage.lang && localStorage.lang == 'zh') {
         dayNamesShort = ["周日  ", "周一 ", "周二 ", "周三 ", "周四 ", "周五  ", "周六 "];
         titleFormat='yyyy年MM月'
     }
     else {
         dayNamesShort = ["Sunday  ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday  ", "Saturday "];
         titleFormat='yyyy Year MM'
     }
   var res="";
   $('#calendar').fullCalendar({
         header: {
           left: 'prev,next',
           center: 'title',
           right: 'today'
       },
       titleFormat:titleFormat,
       dayNamesShort: dayNamesShort,
       defaultDate: '',
       lang: 'zh-cn',
       buttonIcons: false, // show the prev/next text
       weekMode: 'liquid',
       events:function(start,end,callback){
           if (localStorage.lang && localStorage.lang == 'en') {
               $(".fc-header-title h4").html($(".fc-header-title h4").text()+" Month")
               $('.fc-button-today').html('today')
           }
           var id=$('input[name="id"]').val();
           if(id)
             url = "/api/statistic/student/"+id;
           else
             url = "/api/statistic/student";
           $.ajax({
             url:url,
             type:"get",
             success:function(data){
               console.log(data)
              var event=[];
              $.each(data.data,function(i,t){
                // function getTitil(period, name) {
                //   var html = "";
                //       html = "<p>";
                //       html += t.period;
                //       html += "</p>";
                //       html = "<p>";
                //       html += t.name;
                //       html += "</p>";
                //       return html;
                //   }
              event.push({
                       title: t.period+' '+t.name,
                       //title: html,
                       start: t.begintime, // will be parsed
                       end:t.endtime,
                       id:t.id,
                       backgroundColor:"#33c7c6",
                       textColor:"#fff"


                 });

               });
              callback(event);
              for(var i=0; i<event.length;i++){
                var _id=event[i].id;

              }
             }
          });

       },
      //  eventRender: function (event, element) {
      //      element.html(event.title);
      //  }

   })
 }
//搜索
$("#J_teacher_search").on('click',function(){
  var searchInput=$("#searchInput").val();
  ajaxMethod({
      url: '/api/students',
      data:{
          name:searchInput,
          mobile:searchInput
      },
      type: 'get',
      dataType: 'json',
      success: function (data) {
          studentChedule(data);
      }
    })
    function studentChedule(data) {
        console.log(data)
       // var $row = $('.row').html('');
        var $studentC = $('.studentC').html("")
        $.each(data, function (index, item) {
            console.log(item)
            function getDurationMinute(begindate, enddate) {
                if (begindate != null || enddate != null) {
                    var beginTime = +new Date(begindate.replace('T', ' '));
                    var endTime = +new Date(enddate.replace('T', ' '));
                    return (endTime - beginTime) / (60 * 1000) + '分钟';
                } else {
                    return '--';
                }
            }

            var id = item.studentuid;
            var $tr = $('<tr class="" data-uid=' + item.id +' style="height:45px;"></tr>').prependTo($studentC);
            var $td = $('<td ></td>').appendTo($tr);
            var $detailedinformation = $('<a  data-uid='+id+'   data-name='+item.name+'>'+item.name+'</a>').appendTo($td);
            $tr.append('<td>' + item.mobile + '</td>')
            $tr.find('td').css('font-size', '13px');
            $tr.on('click',function(){
            $tr.addClass('active').siblings().removeClass('active');
              var name=$tr.find($detailedinformation).attr('data-name');
              var studentName=$('#studentName').html(name);
              if($("#calendar").html!=""){
                $("#calendar").html("");
                $('input[name="id"]').val(item.studentuid);
                getfullCalendar();
              }
            })
          })
        }
      })

})
